<script setup>
import { ref, onMounted, reactive } from "vue";
import {
  subjectIndex,
  yuangongJl,
  subjectSave,
  subjectShanchu,
  baomingIndex1,
} from "@/api";
import tableValue from "@/components/tableValue.vue";

// 传递子组件表格数据
const tableData = reactive([]);
const baominginformation = ref(true);
// 传递子组件的列配置数组
const columns = [
  { prop: "id", label: "序号", width: "150" },
  { prop: "name", label: "名称", width: "200" },
  { prop: "time", label: "时间", width: "200" },
  { prop: "timelong", label: "时长", width: "160" },
  { prop: "jiaolian", label: "教练", width: "160" },
  // 操作列（特殊列，无 prop）
  { label: "操作", type: "action" },
];

const formRef = ref();
// 信息条数
const total = ref(1);
// 搜索的字段信息
const SearchValue = ref("");
// 教练
const jiaolian = ref({});
onMounted(async () => {
  hyglTable();
});
// 获取教练信息，渲染表单
const jl = async () => {
  const res = await yuangongJl();
  jiaolian.value = res.data;
  // Object.assign(jiaolian, res.data);
  // console.log(res);
};
// console.log(jiaolian.value);

// 请求会员数据
const hyglTable = async (names = "", page = null) => {
  const res = await subjectIndex({
    name: names,
    pageNum: page || 1,
    pageSize: 5,
  });
  // console.log(names);

  total.value = res.total;
  tableData.length = 0;
  Object.assign(tableData, res.data);
  // console.log(total.value);
};
// console.log(total.value);

// 搜索功能
const searchType = (value) => {
  SearchValue.value = value;
  hyglTable(value.value);
};

// 新增功能
const dialogFormVisible = ref(false);

// 判断是新增还是编辑
const RowType = ref("");

// 点击了新增按钮触发
const adds = async () => {
  RowType.value = "课程新增";
  form.value.status = "正常";
  dialogFormVisible.value = true;
  jl();
};
// 默认表单原始数据
// const initialForm = reactive({
// id: "4";
// jiaolian: "张晚意";
// jiaolianid: "5";
// name: "塑形";
// time: "2023-11-22";
// timelong: "57";
// });
// const form = ref({ ...initialForm });

// 表单数据
const form = ref({});

const delshow = ref(false); // 用show变量控制删除弹窗，默认关闭
// 点击了确定按钮触发,会员新增
const submit = () => {
  console.log("表单提交");
  formRef.value.validate(async (valid) => {
    if (valid) {
      dialogFormVisible.value = false;
      console.log("表单验证通过");
      if (RowType.value === "课程新增") {
        const res = await subjectSave(form.value);
        if (res.code === 200) ElMessage.success(res.msg);
      } else {
        const res = await subjectSave(form.value);
        if (res.code === 200) ElMessage.success(res.msg);
      }
      hyglTable("", pagess.value);
      // 处理表单数据
      formRef.value.resetFields();
      form.value = {};
    } else {
      console.log("表单验证失败");
      return false;
    }
  });
};
// 编辑
const editType = (item) => {
  RowType.value = "课程编辑";
  dialogFormVisible.value = true;
  jl();
  Object.assign(form.value, item);
};

// 会员删除
const delId = ref();
const deletes = (item) => {
  delId.value = item.id;
  delshow.value = true;
};
const handleConfirm = async () => {
  const res = await subjectShanchu({ id: delId.value });
  if (res.code === 200) ElMessage.success(res.msg);
  delshow.value = false;
  hyglTable("", pagess.value);
};

const rules = reactive({
  name: [{ required: true, message: "输入不能为空", trigger: "blur" }],
  time: [{ required: true, message: "输入不能为空", trigger: "blur" }],
  timelong: [{ required: true, message: "输入不能为空", trigger: "blur" }],
  jiaolian: [{ required: true, message: "输入不能为空", trigger: "blur" }],
});
const pagess = ref(0);
const Page = async (pages) => {
  await hyglTable("", pages);
  pagess.value = pages;
};

// 课程报名信息
const kechenbaomings = ref();
const dialogTableVisible = ref(false);
const baomingxx = async (item) => {
  dialogTableVisible.value = true;
  // console.log(item);
  const res = await baomingIndex1({ kcid: item.id });
  // Object.assign(kechenbaomings, res.data);
  kechenbaomings.value = res.data;
};
// console.log(kechenbaomings.value);
</script>

<template>
  课程管理
  <tableValue
    :textList="tableData"
    :columns="columns"
    :total="Number(total)"
    :baominginformation="baominginformation"
    @SearchValue="searchType"
    @add="adds"
    @edit="editType"
    @delete="deletes"
    @PageType="Page"
    @baoming="baomingxx"
  />
  <el-dialog v-model="dialogTableVisible" title="报名信息 " width="800">
    <el-table :data="kechenbaomings">
      <el-table-column label="序号" width="200">
        <template #default="scope">
          <!-- scope.$index 就是当前行在数组中的下标（从 0 开始） -->
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column property="huiyuanname" label="学员" width="200" />
    </el-table>
  </el-dialog>

  <el-dialog v-model="dialogFormVisible" :title="RowType" width="800">
    <el-form :model="form" ref="formRef" :rules="rules">
      <el-form-item label="名 称： " prop="name">
        <el-input v-model="form.name" autocomplete="off" />
      </el-form-item>

      <el-form-item label="时 长： " prop="timelong">
        <el-input v-model="form.timelong" autocomplete="off" />
      </el-form-item>
      <el-form-item label="状 态： " prop="jiaolian">
        <el-radio-group v-model="form.jiaolian">
          <el-radio
            v-for="item in jiaolian"
            :key="item.id"
            :value="item.name"
            size="large"
            >{{ item.name }}</el-radio
          >
        </el-radio-group>
      </el-form-item>
      <el-form-item label="开始时间： " prop="time">
        <el-date-picker
          v-model="form.time"
          type="date"
          placeholder="Pick a day"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submit"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>

  <el-dialog v-model="delshow" title="会员删除" width="500" align-center>
    <span>此操作将永久删除该数据，删除后不可恢复，确定要继续吗？</span>
    <template #footer>
      <el-button @click="delshow = false">取消</el-button>
      <el-button type="primary" @click="handleConfirm">确定</el-button>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss"></style>
